<?php

namespace App\Utils;

use App\Models\BookOrder;
use App\Models\WithdrawOrders;
use Overtrue\LaravelWeChat\Facade;

/**
 * Class WechatTemplate
 * @package App\Utils
 * 微信公众号 模板消息工具类
 */
class WechatTemplate
{
    /**
     * @param $bookOrders
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * 订餐成功模版消息
     */
    public static function sendBookSuccess($bookOrders)
    {
        $appid = config('wechat.mini_program.default.app_id');
        $templateId = config('wechat.official_account.default.book_success_template_id');
        $officialOpenid = $bookOrders[0]->user->official_openid;
        if (isset($appid) && isset($templateId) && isset($officialOpenid)) {
            $createdAt = $bookOrders[0]->created_at;
            $balance = $bookOrders[0]->user->balance;
            $eatDate = '';
            foreach ($bookOrders as $k => $v) {
                if ($k == 0) {
                    $eatDate = $eatDate . $v->eat_date;
                } else {
                    $eatDate = $eatDate . '/' . $v->eat_date;
                }
            }
            $realPrice = array_reduce($bookOrders, function ($carry, $order){
                return $order->real_price + $carry;
            }, 0);
            $first = <<<first
您好，您已下单成功，祝您生活愉快！
送餐日期：$eatDate
first;
            $app = Facade::officialAccount();
            return $app->template_message->send([
                'touser' => $officialOpenid,
                'template_id' => $templateId,
                'url' => '',
                "miniprogram" => [
                    "appid" => $appid,
                    "pagepath" => "/pages/order-list/main"
                ],
                'data' => [
                    'first' => $first,
                    'keyword1' => '唯绿农产品学生营养套餐',
                    'keyword2' => $realPrice . '元',
                    'keyword3' => $balance . '元',
                    'remark' => '下单时间：' . $createdAt,
                ]
            ]);
        }
    }

    /**
     * @param BookOrder $bookOrder
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * 订单取消发送模版消息
     */
    public static function sendBookCancel(BookOrder $bookOrder)
    {
        $appid = config('wechat.mini_program.default.app_id');
        $templateId = config('wechat.official_account.default.book_cancel_template_id');
        $officialOpenid = $bookOrder->user->official_openid;
        if (isset($appid) && isset($templateId) && isset($officialOpenid)) {
            $app = Facade::officialAccount();
            return $app->template_message->send([
                'touser' => $officialOpenid,
                'template_id' => $templateId,
                'url' => '',
                "miniprogram" => [
                    "appid" => $appid,
                    "pagepath" => "/pages/order-list/main"
                ],
                'data' => [
                    'first' => '您于' . $bookOrder->updated_at . '取消了' . $bookOrder->eat_date . '的订单',
                    'keyword1' => '唯绿农产品学生营养套餐',
                    'keyword2' => $bookOrder->order_no,
                    'remark' => '欢迎您的再次预订'
                ]
            ]);
        }
    }

    /**
     * @param WithdrawOrders $order
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * 申请退款发送模版消息
     */
    public static function sendWithdrawOrder(WithdrawOrders $order)
    {
        $appid = config('wechat.mini_program.default.app_id');
        $templateId = config('wechat.official_account.default.withdraw_order_template_id');
        $officialOpenid = $order->user->official_openid;
        if (isset($appid) && isset($templateId) && isset($officialOpenid)) {
            $balance = $order->user->balance;
            $first = <<<first
商家已同意您的退款申请，退款金额将在3 - 5个工作日内退还
账户余额: $balance 元
first;
            $app = Facade::officialAccount();
            return $app->template_message->send([
                'touser' => $officialOpenid,
                'template_id' => $templateId,
                'url' => '',
                "miniprogram" => [
                    "appid" => $appid,
                    "pagepath" => "/pages/refund-list/main"
                ],
                'data' => [
                    'first' => $first,
                    'keyword1' => $order->money . ' 元',
                    'keyword2' => '唯绿农产品学生营养套餐',
                    'keyword3' => $order->order_no,
                    'remark' => '申请退款时间: ' . $order->created_at
                ]
            ]);
        }
    }
}